﻿using System;
using System.Collections.Generic;
using System.Text;

using KaixinRobot;
using Majestic12;

namespace KaixinHouse
{
    //所有组件的基类
    public class Component
    {

        protected Account account = null;
        protected UserIdentity identity = null;
        protected UserConfig config = null;

        //public string verifyCode = null;

        protected int cash = 0;
        protected int capital = 0;

        private const int MINCASH = 50000; //维持用户帐户运行，要求该用户最低拥有的金钱数，若低于此资金，robot会自动使用户收益最大化

        public Component()
        {
            this.config = UserConfig._default;
        }

        public Component(Account acc)
        {
            this.account = acc;
            this.identity = acc.identity;
            this.config = acc.config;
        }

        public virtual bool Action() { return true; }

        protected bool isAPoorMan()
        {
            return (this.cash < Component.MINCASH);
        }


        // 登录账号 并获取用户 ID
        public static bool logon(UserIdentity acid)
        {
            acid.userName = null;
            acid.userIdx = null;

            if ((String.IsNullOrEmpty(acid.userMail)) || (String.IsNullOrEmpty(acid.userPass)))
            {
                return false;
            }
            TraceLog.Print("Login as [{0}]: ", acid.userMail);

            string content = KXWeb.Logon(acid.userMail, acid.userPass);

            string strId;

            if ((strId = (new TextParser(content)).getStrBtw("<strong class=\"f14\">", "</strong>")) != null)
            {
                acid.userName = strId;
            }
            else
            {
                acid.userName = "";
            }


            if (content.IndexOf("<title>我的首页 - 开心网</title>") != -1)
            {
                acid.userIdx = (new TextParser(content)).getStrBtw("我的开心网ID:", "\"");
                TraceLog.PrintLn("Ok!");
                TraceLog.PrintLn("Id={0}, Name={1}", acid.userIdx, acid.userName);
                return true;
            }
            else
            {
                TraceLog.Error("Failed!");
                return false;
            }
        }

    }
}
